﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JieBooLaw.WebAdmin
{
    using JieBooLaw.Common.Web.Util;
    using JieBooLaw.Common.Web;
    using JieBooLaw.Domain;
    using JieBooLaw.Service;
    using Ext.Net;
    using JieBooLaw.Common.Util;
    using JieBooLaw.Common.Security;
    using JieBooLaw.Common.Constant;

    public partial class ReleaseInfo : BaseWebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BindData();
                this.BindPItem();
            }
        }

        protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.BindData();
        }

        private void BindData()
        {
            var store = this.GridPanel1.GetStore();

            store.DataSource = GetAllEntity();
            store.DataBind();
        }

        private IList<TInfoRelease_Info> GetAllEntity()
        {
            TInfoRelease_InfoSvc svc = new TInfoRelease_InfoSvc();
            IList<TInfoRelease_Info> list = svc.GetAllList();

            return list;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            TInfoRelease_Info info = new TInfoRelease_Info()
            {
                InfoAuthor = txtInfoAuthor.Text,
                InfoBody = txtInfoBody.Text,
                InfoIcon = txtInfoIcon.Text,
                InfoID = string.IsNullOrEmpty(txtInfoID.Text) ? 0 : ConvertUtil.ToInt(txtInfoID.Text.Trim()),
                InfoKeyword = txtInfoKeyword.Text,
                InfoOrigin = txtInfoOrigin.Text,
                InfoTitle = txtInfoTitle.Text,
                ItemID = ConvertUtil.ToInt(txtPItemID.Value)
            };

            TInfoRelease_InfoSvc svc = new TInfoRelease_InfoSvc();

            TSys_User user = Session[StringConstant.LoginUserInfo] as TSys_User;
            if (string.IsNullOrEmpty(txtInfoID.Text))
            {
                info.Creator = user.UserID;
                info.CreatedDate = DateTime.Now;
                svc.Add(info);
            }
            else
            {
                info.Modifier = user.UserID;
                info.ModifiedDate = DateTime.Now;
                svc.Save(info);
            }

            this.BindData();
            winDetails.Hide();
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            TInfoRelease_InfoSvc svc = new TInfoRelease_InfoSvc();

            string json = e.ExtraParams["Values"];

            Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);

            foreach (Dictionary<string, string> row in companies)
            {
                string infoId = row["InfoID"];
                string itemId = row["ItemID"];
                svc.Delete(new TInfoRelease_Info() { InfoID = ConvertUtil.ToInt(infoId), ItemID = ConvertUtil.ToInt(itemId) });
            }

            this.BindData();
        }

        private void BindPItem()
        {
            var store = this.txtPItemID.GetStore();

            store.DataSource = GetAllItem();
            store.DataBind();
        }

        private IList<TInfoRelease_Item> GetAllItem()
        {
            TInfoRelease_ItemSvc svc = new TInfoRelease_ItemSvc();
            IList<TInfoRelease_Item> list = svc.GetAllList();

            return list;
        }
    }
}
